package com.airnewslinkosky.ticket.ui.lowerlevel;

import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.airnewslinkosky.ticket.R;
import com.airnewslinkosky.ticket.model.OfficeResponseResult;
import com.airnewslinkosky.ticket.net.WebAccessor;
import com.airnewslinkosky.ticket.ui.BaseActivity;
import com.airnewslinkosky.ticket.ui.Main;
import com.airnewslinkosky.ticket.util.Constants;
import com.mobclick.android.MobclickAgent;

/**
 * 下级代理商户查询
 * @author toshiba
 *
 */
public class LowerLevelQuery extends BaseActivity implements OnClickListener{
	public final int NORESULT = 3;
	public final int NOORDER = 4;
	public final int NOORDERSTAT = 5;
	public final int NET_ERROR=6;
	public final int SEARCH_ERROR=7;
	
	private SharedPreferences sp;
	private LinearLayout addLoweLevelLay;
	private Button searchBut;
	//分销商名称,管理员名称
	private EditText distributor,administrator;
	
	private ProgressDialog progressDialog = null;
	public Handler myHandler;
	private Toast toast;
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.lower_level);
		// 删除窗口背景
        getWindow().setBackgroundDrawable(null);
        
		activityList.add(this);
		
		addLoweLevelLay = (LinearLayout) findViewById(R.id.addLoweLevelLay);
		addLoweLevelLay.setOnClickListener(this);
		
		searchBut = (Button) findViewById(R.id.searchBut);
		searchBut.setOnClickListener(this);
		
		distributor = (EditText) findViewById(R.id.distributor);
		administrator = (EditText) findViewById(R.id.administrator);
		
		findViewById(R.id.backBut).setOnClickListener(this);
		
		System.out.println("222222222222222222222");
		
		myHandler = new Handler() {
			@Override
			public void handleMessage(Message msg) {
				switch (msg.what) {
				case NORESULT:
					toast = Toast.makeText(LowerLevelQuery.this, "未找到符合条件的分销商", Toast.LENGTH_LONG);
					toast.setGravity(Gravity.CENTER, 0, 0);
					toast.show();
					break;
				case NOORDER:
					toast = Toast.makeText(LowerLevelQuery.this, "无分销商信息", Toast.LENGTH_LONG);
					toast.setGravity(Gravity.CENTER, 0, 0);
					toast.show();
					break;
				case NET_ERROR:
					toast = Toast.makeText(LowerLevelQuery.this, "网络超时，连接失败", Toast.LENGTH_LONG);
					toast.setGravity(Gravity.CENTER, 0, 0);
					toast.show();
					break;
				case SEARCH_ERROR:
					toast = Toast.makeText(LowerLevelQuery.this,  (String)msg.obj, Toast.LENGTH_LONG);
					toast.setGravity(Gravity.CENTER, 0, 0);
					toast.show();
					break;
				}
				super.handleMessage(msg);
			}
		};
		
	}
	
	
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
			case R.id.addLoweLevelLay:
				Intent intent = new Intent();
				intent.setClass(LowerLevelQuery.this, LowerLevelTabAdd.class);
				startActivity(intent);
				break;
			case R.id.searchBut:
				search();
				break;
			case R.id.backBut: 
				Intent intentR = new Intent();
				intentR.setClass(LowerLevelQuery.this, Main.class);
				startActivity(intentR);
				break;
		}
	}
	
	private void search() {
		final String officeName = distributor.getText().toString();
		final String adminName = administrator.getText().toString();
		
		progressDialog = ProgressDialog.show(this, "请稍等...", "查询分销商中...",true);
		final Thread thread1 = new Thread() {
			public void run() {
//				sp = getSharedPreferences("UserInfoB2B", Context.MODE_WORLD_WRITEABLE | Context.MODE_WORLD_READABLE);
//		        String userIdLogin = sp.getString("userIdB2B", null);
//		        String userPwd =  sp.getString("userPwdB2B", null);
//		        String asToken =   sp.getString("ASTokenB2B", null);
		        String userIdLogin = Constants.USERID;
		        String userPwd =  Constants.USERPWD;
		        String asToken =  Constants.ASToken;
		        
		        System.out.println("userIdLogin="+userIdLogin+";userPwd="+userPwd+";asToken="+asToken);
		        System.out.println("officeName="+officeName+";adminName="+adminName);
		        OfficeResponseResult result = WebAccessor.searchOffice(LowerLevelQuery.this,userIdLogin,userPwd,asToken,officeName,adminName);
				
				if (progressDialog.isShowing()) {
					if (result== null) {
						Message msg = new Message();
						msg.what = NORESULT;
						myHandler.sendMessage(msg);
					}else if(!"0".equals(result.Result)){
						Message msg = new Message();
						msg.what = SEARCH_ERROR;
						msg.obj = result.ErrorDescription;
						myHandler.sendMessage(msg);
					}else{
						Intent intentS = new Intent();
						intentS.setClass(LowerLevelQuery.this, LowerLevelList.class);
						LowerLevelList.OfficeInfos = result.OfficeInfos;
						startActivity(intentS);
					}
					progressDialog.dismiss();
				}
			}
		};
		progressDialog.setCancelable(true);
		thread1.start();
	}
	
	public void onResume() {     
		super.onResume();     
		MobclickAgent.onResume(this); 
	}  
	
	public void onPause() {     
		super.onPause();     
		MobclickAgent.onPause(this); 
	}  
}
